package com.atguigu.lease.web.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.web.admin.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.atguigu.lease.web.admin.service.LeaseTermService;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
@RequiredArgsConstructor
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    private final LeaseAgreementMapper leaseAgreementMapper;
    @Override
    public IPage<AgreementVo> paegAgreementVo(Page<AgreementVo> page, AgreementQueryVo queryVo) {

        return leaseAgreementMapper.paegAgreementVo(page,queryVo);
    }

    @Override
    public AgreementVo getbyidAgreementVo(Long id) {

        LeaseAgreement leaseabyId = getById(id);
        AgreementVo agreementVo= BeanUtil.copyProperties(leaseabyId,AgreementVo.class);
        //根据id查询公寓
        Long apartmentId = leaseabyId.getApartmentId();
        ApartmentInfo apartmentInfo = Db.lambdaQuery(ApartmentInfo.class)
                .eq(ApartmentInfo::getId, apartmentId)
                .one();
        //根据id查询房间
        Long roomId = leaseabyId.getRoomId();
        RoomInfo roomInfo = Db.lambdaQuery(RoomInfo.class)
                .eq(RoomInfo::getId, roomId)
                .one();
        //查询支付方式
        Long paymentTypeId = leaseabyId.getPaymentTypeId();
        PaymentType paymentType = Db.lambdaQuery(PaymentType.class)
                .eq(PaymentType::getId, paymentTypeId)
                .one();
        //根据id查询租期
        Long leaseTermId = leaseabyId.getLeaseTermId();
        LeaseTerm leaseterm = Db.lambdaQuery(LeaseTerm.class)
                .eq(LeaseTerm::getId, leaseTermId)
                .one();
        agreementVo.setLeaseTerm(leaseterm);
        agreementVo.setApartmentInfo(apartmentInfo);
        agreementVo.setPaymentType(paymentType);
        agreementVo.setRoomInfo(roomInfo);
        return agreementVo;
    }
}




